<template>
  <div class="coupon">
    <van-tabs v-model="active">
      <van-tab title="可使用">
        <div class="coupon-input">
          <input type="text" placeholder="请输入优惠码" v-model="code" /><label
            @click="addCouponHandler"
            >兑换</label
          >
        </div>
        <div class="coupon-main">
          <div class="coupon-list" v-for="coupon in getCoupon" :key="coupon.id">
            <div class="coupon-price">
              <p class="price">
                <strong>{{ coupon.valueDesc }}</strong
                >{{ coupon.unitDesc }}
              </p>
              <p class="condition">{{ coupon.condition }}</p>
            </div>
            <div class="coupon-info">
              <p class="title">{{ coupon.name }}</p>
              <p>2020.07.31-2020.08.05</p>
            </div>
            <van-button round type="info" size="mini" color="#ee0a24" to="/sort"
              >立即使用</van-button
            >
          </div>
        </div>
      </van-tab>
      <van-tab title="已失效">
        <div class="coupon-main">
          <div
            class="coupon-list"
            v-for="coupon in getDisCoupon"
            :key="coupon.id"
          >
            <div class="coupon-price" style="color:#323233">
              <p class="price">
                <strong>{{ coupon.valueDesc }}</strong
                >{{ coupon.unitDesc }}
              </p>
              <p class="condition">{{ coupon.condition }}</p>
            </div>
            <div class="coupon-info">
              <p class="title">{{ coupon.name }}</p>
              <p>2020.07.31-2020.8.05</p>
            </div>
          </div>
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>
<script>
import Vue from "vue";
import { Tab, Tabs, Button, Toast } from "vant";
import { mapGetters } from "vuex";

Vue.use(Toast);
Vue.use(Button);
Vue.use(Tab);
Vue.use(Tabs);
export default {
  name: "coupon",
  data() {
    return {
      active: 0,
      code: ""
    };
  },
  computed: {
    // ...mapState(["couponsList"])
    ...mapGetters(["getCoupon"]),
    ...mapGetters(["getDisCoupon"])
  },
  methods: {
    addCouponHandler() {
      if (this.code) {
        let num = Math.random().toFixed(1);
        const coupon = {
          id: this.code,
          available: 1,
          condition: "无使用门槛",
          value: num * 100,
          name: "高富帅专用",
          valueDesc: num,
          unitDesc: "元",
          startAt: 1489104000,
          endAt: 1514592000,
          userName: this.$store.state.userInfo[0].username,
          isUsed: false
        };
        this.$store.commit("addCoupon", coupon);
      } else {
        // console.log("null")
        Toast("请输入兑换码");
      }
      this.code = "";
    }
  }
};
</script>
<style lang="scss" scoped>
.van-tab__pane {
  width: 100%;
  height: 530px;
  overflow: auto;
}
.coupon-input {
  padding: 10px 0;
  width: 100%;
  input {
    border-radius: 20px;
    border: none;
    background: #f7f8fa;
    padding: 10px;
    margin-left: 20px;
    color: #323233;
    width: 70%;
  }
  label {
    color: #ee0a24;
    margin-left: 20px;
  }
}
.coupon-main {
  background: #f7f8fa;
  width: 100%;
  height: 100%;

  .coupon-list {
    background: #fff;
    position: relative;
    height: 100px;
    border-radius: 5px;
    margin: 10px;

    button {
      position: absolute;
      right: 10px;
      top: 35px;
    }

    .coupon-price {
      position: absolute;
      color: #ee0a24;
      top: 20px;
      left: 20px;

      .price {
        text-align: center;
        margin-bottom: 6px;

        strong {
          font-weight: 500;
          font-size: 30px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          margin-right: 2px;
        }
      }
      .condition {
        font-size: 12px;
        line-height: 16px;
        white-space: pre-wrap;
      }
    }
    .coupon-info {
      position: absolute;
      top: 28px;
      right: 80px;

      .title {
        margin-bottom: 10px;
        font-weight: bold;
        font-size: 14px;
        line-height: 20px;
      }
    }
  }
}
</style>
